<?php

namespace app\admin\model\hlyp;

use think\Model;


class Goods extends Model
{

    

    

    // 表名
    protected $name = 'goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'shelf_status_text',
        'is_free_shipping_text',
        'create_time_text',
        'update_time_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getShelfStatusList()
    {
        return ['1' => __('Shelf_status 1'), '0' => __('Shelf_status 0')];
    }

    public function getIsFreeShippingList()
    {
        return ['1' => __('Is_free_shipping 1'), '0' => __('Is_free_shipping 0')];
    }


    public function getShelfStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shelf_status']) ? $data['shelf_status'] : '');
        $list = $this->getShelfStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsFreeShippingTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_free_shipping']) ? $data['is_free_shipping'] : '');
        $list = $this->getIsFreeShippingList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
